<?php
/**
==============================================================================
 * Class urlComponent GracePHP的URL组件类
 * @author 肖永标
 * @date 2014-12-05
==============================================================================
 */
 
class urlComponent extends Component {
    
    /** 获取当前控制器名称
     * @return string
     */
	public static function getController()
	{
		if(isset($_SERVER['PATH_INFO']))
		{
			$pathInfo = substr($_SERVER['PATH_INFO'], 1);
			$pathInfoArr = explode('/', $pathInfo);

            if(empty($pathInfoArr[0]))
                return Config::getItem('defaultController') . 'Controller';

			return $pathInfoArr[0] . 'Controller';
		}
		else
		{
			$controller = Config::getItem('defaultController') . 'Controller';
			return $controller;
		}
	}
    

    /** 获取pathInfo模式下的url各个分段
     *  @param int $n 需要获取第几段
     *  @param mixed $default 段数不存在时的默认值
     *  @return string 
     */
    public function segment($n, $default='')
    {
        $pathInfo =
            isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : 
                                            Config::getItem('defaultController') . '/index';

        $pathInfoArr = explode('/', $pathInfo);

        if($pathInfoArr[$n])
            return $pathInfoArr[$n];
        else
            return $default ? $default : false;
    }


    /** 创建一个友好帅气的URL
     * @param string $route 路由 eg:Index/index
     * @param array $params 路由后面携带的查询参数
     * @return string
     */
    public function createUrl($route, $params=array())
    {
        $scriptName = '';

        if(Config::getItem('friendUrl.hiddenIndex'))
        {
            $scriptName = $_SERVER["SERVER_NAME"];
            $temp = explode('/', substr($_SERVER["SCRIPT_NAME"], 1));
            if(sizeof($temp) > 1)
                $scriptName .= '/' . $temp[0];

            $scriptName = $scriptName . '/';
        }
        else
        {
            $scriptName = $_SERVER["SERVER_NAME"] . $_SERVER["SCRIPT_NAME"] . '/';
        }

        $suffix = '';

        if(Config::getItem('friendUrl.suffix') && Config::getItem('pathInfo'))
            $suffix = Config::getItem('friendUrl.suffix');

        $routeArr = explode('/', $route);

        $query = '';

        if(sizeof($params) != 0)
        {
            $query = '/';
            foreach($params as $k=>$v)
            {
                if (Config::getItem('pathInfo'))
                    $query .= $k . '/' . $v . '/';
                else
                    $query .= '&' . $k . '=' . $v;
            }
            if (Config::getItem('pathInfo'))
                $query = substr($query, 0, -1);
        }

        if (Config::getItem('pathInfo'))
            return 'http://' . $scriptName . $routeArr[0] . '/' . $routeArr[1] . $query .  $suffix;
        else
            return 'http://' . $scriptName . '?c=' . $routeArr[0] . '&a=' . $routeArr[1] . $query;
    }


    /** 成功跳转
     * @param string $msg 信息提示
     * @param array $routeAndParams 路由和参数
     * @param int $jumpTime 跳转等待时间
     */
    public function success($msg, $routeAndParams=array(), $jumpTime=3)
    {
        $this->_location($msg ,$routeAndParams, $jumpTime, 'success');
    }


    /** 失败跳转
     * @param string $msg 信息提示
     * @param array $routeAndParams 路由和参数
     * @param int $jumpTime 跳转等待时间
     */
    public function fail($msg, $routeAndParams=array(), $jumpTime=3)
    {
        $this->_location($msg, $routeAndParams, $jumpTime, 'fail');
    }


    /** 普通跳转
     * @param array $routeAndParams 路由和参数
     */
    public function redirect($routeAndParams=array())
    {
        $route = $routeAndParams[0];
        $params = $routeAndParams[1] ? $routeAndParams[1] : '';

        $redirectUrl = $this->createUrl($route, $params);
        header('Location:'.$redirectUrl);
    }


    /** 是否允许被加载
     * @return bool
     */
    public function allowLoad()
    {
        return true;
    }


    /** 失败和成功跳转的公共方法
     * @param string $msg 信息提示
     * @param array $routeAndParams 路由和参数
     * @param int $jumpTime 跳转等待时间
     * @param string $action 成功还是失败行为
     */
    private function _location($msg, $routeAndParams=array(), $jumpTime, $action)
    {
        $var['redirect'] = $_SERVER['HTTP_REFERER'];

        if($routeAndParams)
        {
            $route = $routeAndParams[0];
            $params = $routeAndParams[1] ? $routeAndParams[1] : '';

            $var = array();
            $redirectUrl = $this->createUrl($route, $params);
            $var['redirect'] = $redirectUrl;
        }

        $var['msg'] = $msg;
        $var['jumpTime'] = $jumpTime;

        extract($var);

        include SYSTEM . '/views/'.$action.'.php';
        exit;
    }
}
